import java.util.*;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public static TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    // 前序遍历
    public static void preOrder(TreeNode root) {
        if(root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);

    }

    // 中序遍历
    public static void inOrder(TreeNode root) {
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public static void postOrder(TreeNode root) {
        if(root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static int nodeSize = 0;

    /**
     * 获取树中节点的个数：遍历思路
     */
    public static void size(TreeNode root) {
        if(root == null) return;
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    public static int size2(TreeNode root) {
        if(root == null) return 0;
        return size2(root.left) + size2(root.right) + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    public static void getLeafNodeCount1(TreeNode root) {
        if(root == null) return;
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    public static int getLeafNodeCount2(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    public static int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k-1) + getKLevelNodeCount(root.right, k-1);
    }

    // 检测值为value的元素是否存在
    public static TreeNode find(TreeNode root, char val) {
        if(root == null) return null;
        if(root.val == val) {
            return root;
        }
        TreeNode leftVal = find(root.left, val);
        if(leftVal != null) {
            return leftVal;
        }
        TreeNode rightVal = find(root.right, val);
        if(rightVal != null) {
            return rightVal;
        }
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if(root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.println(top.val + " ");
            if (top.left != null) {
                queue.offer(top.left);
            }
            if (top.right != null) {
                queue.offer(top.right);
            }
        }
    }

    /*public List<List<Integer>> levelOrder2(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) {
            queue.offer(root);
        }
        List<List<Integer>> ret = new ArrayList<>();

        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0) {
                TreeNode top = queue.poll();
                list.add(top.val);
                if (top.left != null) {
                    queue.offer(top.left);
                }
                if (top.right != null) {
                    queue.offer(top.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }*/


    // 判断一棵树是不是完全二叉树
    public static boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) {
            queue.offer(root);
        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                return false;
            }
        }
        return true;
    }
    // 判断两棵树是不是相同的树
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if(p != null && q == null || p == null && q != null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

    // 反转二叉树
    public static TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    // 判断另一棵树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }
        if(isSameTree(root, subRoot)) {
            return true;
        }
        if(isSubtree(root.left, subRoot)) {
            return true;
        }
        if(isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;
    }
    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    public static int getHeight1(TreeNode root) {
        if(root == null) return 0;
        int leftH = getHeight1(root.left);
        int rightH = getHeight1(root.right);
        return (leftH > rightH ? leftH : rightH) + 1;
    }
    // 判断一棵树是否为平衡二叉树
    public boolean isBalanced1(TreeNode root) {
        if(root == null) return true;
        int leftHight = getHeight1(root.left);
        int rightHight = getHeight1(root.right);

        return Math.abs(leftHight - rightHight) < 2 && isBalanced1(root.left) && isBalanced1(root.right);
    }
    // 求树的高度
    public static int getHeight(TreeNode root) {
        if(root == null) return 0;
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        if(leftH >= 0 && rightH >= 0 && Math.abs(leftH - rightH) <= 1) {
            return Math.max(leftH, rightH) + 1;
        }else {
            return -1;
        }
    }
    // 判断一棵树是否为平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        return getHeight(root) >=0;
    }
    // 判断一棵树是否为对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSymmetricChild(root.left, root.right);
    }
    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {
        if(leftTree != null && rightTree == null || leftTree == null && rightTree != null) {
            return false;
        }
        if(leftTree == null && rightTree == null) {
            return true;
        }
        if(leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left, rightTree.right) && isSymmetricChild(leftTree.right, rightTree.left);
    }
    // 方法二 求最近公共祖先
    public static TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        Stack<TreeNode> s1 = new Stack<>();
        Stack<TreeNode> s2 = new Stack<>();

        getPath(root, p, s1);
        getPath(root, q, s2);

        int size1 = s1.size();
        int size2 = s2.size();
        if(size1 > size2) {
            int size = size1 - size2;
            while(size != 0) {
                s1.pop();
                size--;
            }
        }else {
            int size = size2 - size1;
            while(size != 0) {
                s2.pop();
                size--;
            }
        }
        while(!s1.empty() && !s2.empty()) {
            TreeNode tmp1 = s1.pop();
            TreeNode tmp2 = s2.pop();
            if(tmp1 == tmp2) {
                return tmp1;
            }
        }
        return null;
    }

    /**
     * 在root这棵树当中 找到node这个节点上的位置
     * @param root
     * @param node
     * @return
     */
    public static boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }
        boolean ret = getPath(root.left, node, stack);
        if(ret == true) {
            return true;
        }
        boolean ret2 = getPath(root.right, node, stack);
        if(ret2 == true) {
            return true;
        }
        stack.pop();
        return false;
    }

    // 非递归实现前序遍历
    public static void preOrderNor(TreeNode root) {
        if(root == null) return;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            //cur == null
            TreeNode top = stack.pop();
            cur = top.right;
        }

    }
    public static void main(String[] args) {
        TreeNode root = createTree();
        System.out.println("====前序遍历====");
        preOrder(root);
        System.out.println();
        preOrderNor(root);
        System.out.println();
        System.out.println("====中序遍历====");
        inOrder(root);
        System.out.println();
        System.out.println("====后序遍历====");
        postOrder(root);
        System.out.println();
        System.out.println("树中节点个数为: ");
        size(root);
        System.out.println(nodeSize);
        System.out.println(size2(root));
        System.out.println("树中的叶子节点个数为: ");
        getLeafNodeCount1(root);
        System.out.println(leafSize);
        System.out.println(getLeafNodeCount2(root));
        System.out.println("第k层的节点个数为: ");
        System.out.println(getKLevelNodeCount(root, 4));
        System.out.println("树的高度为: ");
        System.out.println(getHeight(root));
        System.out.println("检查元素是否存在: ");
        System.out.println(find(root, 'I').val);
        Stack<TreeNode> stack = new Stack<>();
        getPath(root, root.left.right.right, stack);
    }
}